package cn.xuewenbao.edu.controller;

import cn.xuewenbao.edu.entity.LearnRecord;
import cn.xuewenbao.edu.entity.WeekLearnTime;
import cn.xuewenbao.edu.service.LearnRecordService;
import cn.xuewenbao.edu.service.WeekLearnTimeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/edu/learn-record")
@Api(tags = "学习记录")
@Slf4j
public class LearnRecordController {

    @Autowired
    private LearnRecordService learnRecordService;

    @Autowired
    private WeekLearnTimeService weekLearnTimeService;

    @ApiOperation(value = "添加或更新学习记录")
    @PostMapping("/updateLearnRecord")
    public Result<?> updateLearnRecord(@RequestBody LearnRecord learnRecord) {
        return learnRecordService.updateLearnRecord(learnRecord);
    }

    /**
     * @param  map: userId(String), columnType(String)
     */
    @ApiOperation(value = "查找学习记录(根据内容类型)")
    @PostMapping("/getLearnRecord")
    public Result<?> getLearnRecord(@RequestBody Map<String, String> map) {
        String userId = map.get("userId");
        String columnType = map.get("columnType");

        if (StringUtils.isBlank(userId) || StringUtils.isBlank(columnType)
                || !StringUtils.isNumeric(userId) || !StringUtils.isNumeric(columnType)) {
            return Result.error("参数格式不正确");
        }

        return learnRecordService.getLearnRecord(userId, columnType);
    }

    @ApiOperation(value = "添加或更新每周学习时间")
    @PostMapping("/updateWeekLearnTime")
    public Result<?> updateWeekLearnTime(@RequestBody WeekLearnTime weekLearnTime) {
        return weekLearnTimeService.updateWeekLearnTime(weekLearnTime);
    }
}
